const URL_PREFIX = "http://www.pudge.wang:4000";

const http = {
  get(url, params) {
    // 判断是否有params 有没有传进来
    if (params) {
      const arr = Object.keys(params).map((item) => {
        return item + "=" + params[item];
      });
      const str = "?" + arr.join("&"); //["a=3","b=4"]
      url += str;
    }

    return new Promise((resolve) => {
      fetch(URL_PREFIX + url)
        .then((response) => response.json())
        .then((res) => {
          resolve(res);
        });
    });
  },

  post(url, data) {
    return new Promise((resolve) => {
      fetch(URL_PREFIX + url, {
        body: JSON.stringify(data),
        headers: {
          "content-type": "application/json",
        },
        method: "POST", // get post put
      })
        .then((response) => response.json())
        .then((res) => resolve(res));
    });
  },
};
export default http; //将http暴露出去 便于使用
